package cn.bjc.pet.service.impl;

import cn.bjc.basic.service.impl.BaseServiceImpl;
import cn.bjc.pet.domain.Pet;
import cn.bjc.pet.domain.PetDetail;
import cn.bjc.pet.mapper.PetDetailMapper;
import cn.bjc.pet.mapper.PetMapper;
import cn.bjc.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    
    @Autowired
    private PetDetailMapper petDetailMapper;
    
    
    @Override
    public void save(Pet pet) {
        //先添加pet,返回自增长主键
        petMapper.save(pet);

        //再添加pet_detail
        PetDetail petDetail = pet.getPetDetail();
        petDetail.setPet_id(pet.getId());
        petDetailMapper.save(petDetail);
    }

    @Override
    public void update(Pet pet) {
        //pet
        petMapper.update(pet);
        
        //detail
        petDetailMapper.update(pet.getPetDetail());
    }

    //上架
    @Override
    public void onSale(List<Long> ids) {
        /**
         * 思路:
         *  1.审核
         *  1.改状态   sql中:state=1
         *  2.写入上架时间:创建一个当前时间.数据库写入
         *  传参方式:1.对象,2.map,3.直接写两个参数
         */
        //审核,通过则上架,没过则自定义异常
        
        //
        Date date = new Date();
        petMapper.onSale(ids,date);
        
    }

    //下架
    @Override
    public void offSale(List<Long> ids) {
        /**
         * 思路:
         *  1.改状态   sql中:state=0
         *  2.写入下架时间:创建一个当前时间.数据库写入
         *  传参方式:1.对象,2.map,3.直接写两个参数
         */
        Date date = new Date();
        petMapper.offSale(ids,date);
    }

    @Override
    public void remove(Long id) {

        super.remove(id);
        // 详情
        petDetailMapper.removeByPetId(id);
    }
}
